#pragma once

#include <cv.h>
#include <list>
#include <string>

// OCREngine - optical recognition engine.
// used to translate a single image of a digit to a digit (numeric).
// scenario of use: first the class MUST be initialized somewhere, using init procedure which recieves as 
//                  input the dir containing the digit database (the dir must contain an "index" file, written 
//                  in the correct format, along with the images).
//                  once the class has been initialized it can be used to perform ocr on single digits.

class OCREngine
{
	bool m_init;

	typedef std::pair<IplImage*, int> t_imgmember;
	typedef std::list< t_imgmember > t_imglist;
	t_imglist m_db;

	// this function computes the sum of the squarted error between the two images (per pixel)
	unsigned long computeSquareError (IplImage* one, IplImage* two) const;

	OCREngine(void);
	~OCREngine(void);
public:
	// returns the singleton instance of the class
	static OCREngine *instance (void) {
		static OCREngine eng;
		return &eng;
	}
	// initilizes the database (MUST BE CALLED BEFORE USE). the dir must end with a '/' and contain an "index" file, along with the needed images.
	bool init (const std::string &digitdir);
	// identifies the image recieved (by computing its total square error vs each of the database images, and deciding by that the "closest" digit to the image we have)
	int identify (const IplImage* digit) const;
};
